package com.ruoyi.web.controller.danger;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.opio.util.ExampleBase;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysPlan;
import com.ruoyi.system.entity.SysGasPoint;
import com.ruoyi.system.service.SysGasPointService;
import freemarker.template.TemplateException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 可燃有毒气体点位(SysGasPoint)表控制层
 *
 * @author makejava
 * @since 2022-12-01 16:48:05
 */
@RestController
@RequestMapping("sysGasPoint")
public class SysGasPointController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private SysGasPointService sysGasPointService;

    /**
     * 分页查询
     * @param sysGasPoint 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(SysGasPoint sysGasPoint) {
        startPage();
        List<SysGasPoint> sysGasPoints = this.sysGasPointService.queryByPage(sysGasPoint);
        return getDataTable(sysGasPoints);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.sysGasPointService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysGasPoint 实体
     * @return 新增结果
     */
    @PostMapping("/create")
//    @PreAuthorize("@ss.hasPermi('sysGasPoint:create')")
    public AjaxResult add(@RequestBody SysGasPoint sysGasPoint) {
        return AjaxResult.success(this.sysGasPointService.insert(sysGasPoint));
    }

    /**
     * 编辑数据
     *
     * @param sysGasPoint 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
//    @PreAuthorize("@ss.hasPermi('sysGasPoint:update')")
    public AjaxResult edit(@RequestBody SysGasPoint sysGasPoint) {
        return AjaxResult.success(this.sysGasPointService.update(sysGasPoint));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
//    @PreAuthorize("@ss.hasPermi('sysGasPoint:delete')")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.sysGasPointService.deleteById(id));
    }

    /**
     * 与实时数据库关联
     * @return
     */
    @GetMapping("/getTimeData")
    public AjaxResult getTimeData(SysGasPoint query) {
        List<SysGasPoint> sysGasPoints = this.sysGasPointService.queryByPage(query);
        Map<String, Object> map = ExampleBase.toResult();
        if (CollUtil.isNotEmpty(map)) {
            sysGasPoints.forEach(sysGasPoint -> {
                boolean b = map.containsKey(sysGasPoint.getDatabaseBitNumber());
                if (b) {
                    Object o = map.get(sysGasPoint.getDatabaseBitNumber());
                    sysGasPoint.setAv(o.toString());
                    sysGasPoint.setUnit("%");
                }
            });
        }
        return AjaxResult.success(sysGasPoints);
    }

    @Log(title = "可燃有毒气体检测点位(", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<SysGasPoint> util = new ExcelUtil<>(SysGasPoint.class);
        List<SysGasPoint> sysGasPoints = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = sysGasPointService.importExcel(sysGasPoints, updateSupport, operName);
        return success(message);
    }


    @Log(title = "可燃有毒气体检测点位(", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysGasPoint sysGasPoint)
    {
        List<SysGasPoint> sysGasPoints = sysGasPointService.queryByPage(sysGasPoint);
        ExcelUtil<SysGasPoint> util = new ExcelUtil<>(SysGasPoint.class);
        util.exportExcel(response, sysGasPoints, "可燃有毒气体检测点位(");
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysGasPoint> util = new ExcelUtil<>(SysGasPoint.class);
        util.importTemplateExcel(response,"可燃有毒气体检测点位(");
    }

    @GetMapping("/createFile")
    public AjaxResult createFile() throws TemplateException, IOException {
        sysGasPointService.createDroolsFile();
        return AjaxResult.success();
    }
    @GetMapping("/reload")
    public AjaxResult reload(){
        sysGasPointService.reload();
        return AjaxResult.success();
    }
}

